﻿using Amba.ULogin.Models;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Common.Models;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Widgets.Models;

namespace Amba.ULogin
{
    public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            SchemaBuilder.CreateTable(typeof (ULoginPartRecord).Name,
                x => x
                    .ContentPartRecord()
                    .Column<string>("RolesForCreatedUsers", c => c.Unlimited()));
    
            SchemaBuilder.CreateTable(typeof(ULoginUserRecord).Name,
                table => table
                    .Column<int>("Id", c => c.PrimaryKey().Identity())
                    .Column<int>("UserId")               
                    .Column<string>("SocialKey", c => c.WithLength(1000))
                    .Column<string>("Network", c => c.WithLength(255))
                    .Column<string>("LastData", c => c.Unlimited())
                    .Column<string>("ExtraUserInfo", c => c.Unlimited())
                );
            SchemaBuilder.AlterTable(typeof (ULoginUserRecord).Name, x => x.CreateIndex("SocialKeyIdx", "SocialKey"));
            SchemaBuilder.AlterTable(typeof(ULoginUserRecord).Name, x => x.CreateIndex("SocialNetworkIdx", "Network"));



            ContentDefinitionManager.AlterPartDefinition(typeof(ULoginPart).Name, x => x.Attachable(true));

            ContentDefinitionManager.AlterTypeDefinition(
                "ULoginWidget", 
                    x => 
                    x.WithPart(typeof(WidgetPart).Name)
                    .WithPart(typeof(ULoginPart).Name)
                    .WithPart(typeof(CommonPart).Name)
                    .WithPart(typeof(IdentityPart).Name)
                    .WithSetting("Stereotype", "Widget"));

            return 1;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.CreateTable(typeof(ULoginUserDataPartRecord).Name,
                table => table
                    .ContentPartRecord()
                    .Column<string>("ULoginResponce", x => x.Unlimited())
                    .Column<bool>("IsVerifiedEmail")
                    .Column<string>("Uid")
                    .Column<string>("BDate")
                    .Column<string>("Network")
                    .Column<string>("Profile")
                    .Column<string>("Sex")
                    .Column<string>("Email")
                    .Column<string>("LastName")
                    .Column<string>("FirstName")
                    .Column<string>("Nickname")
                    .Column<string>("UserIdentity")
                    .Column<string>("PhotoBig")
                    .Column<string>("Photo")
                    .Column<string>("Phone")
                    .Column<string>("City")
                    .Column<string>("Country")
                );
            ContentDefinitionManager.AlterPartDefinition(typeof(ULoginUserDataPart).Name, x => x.Attachable(true));

            SchemaBuilder.CreateTable(typeof (ULoginSettingsRecord).Name, table => table
                .Column<int>("Id", c => c.PrimaryKey().Identity())
                .Column<bool>("SaveUserPhotoToDisk")
                );

            
            return 2;
        }
    }
}